package com.chenyun.infra.xxljob.admin.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.collection.CollectionUtil;
import com.chenyun.common.vo.AjaxResult;
import com.chenyun.infra.xxljob.admin.core.exception.XxlJobException;
import com.chenyun.infra.xxljob.admin.service.LoginService;
import com.chenyun.infra.xxljob.admin.service.XxlJobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * index controller
 *
 * @author xuxueli 2015-12-19 16:13:16
 */
@RestController
@Api(tags = "xxljob-首页")
//@SaCheckRole("admin")
public class IndexController {

    @Resource
    private XxlJobService xxlJobService;
    @Resource
    private LoginService loginService;


    /**
     * 获取控制台信息
     *
     * @param model
     * @return
     */
    @GetMapping("/dashboard")
    @ApiOperation("获取控制台信息")
    @SaIgnore
    public AjaxResult index() {
        Map<String, Object> dashboardMap = xxlJobService.dashboardInfo();
        return AjaxResult.SUCCESS(dashboardMap);
    }

    @PostMapping("/chartInfo")
    public AjaxResult chartInfo(@RequestBody Map<String,Object> params) {
        //params--->Date startDate, Date endDate
        if (CollectionUtil.isEmpty(params)){
            throw new XxlJobException("参数错误");
        }
        Date startDate =(Date) params.get("startDate");
        Date endDate = (Date)params.get("endDate");

        Map<String, Object> objectMap = xxlJobService.chartInfo(startDate, endDate);
        return AjaxResult.SUCCESS(objectMap);
    }




    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

}
